package com.lh_travel.lohas.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckedTextView;
import android.widget.ImageView;
import android.widget.TextView;

import com.lh_travel.lohas.R;
import com.squareup.picasso.Picasso;

/**
 * Created by aaron on 2018/6/6.
 */

public class ViewHolder {
    private final SparseArray<View> mViews;
    private int mPosition;
    private View mConvertView;
    private Context mContext;

    private ViewHolder(Context context, ViewGroup parent, int layoutId,
                       int position)
    {
        this.mPosition = position;
        this.mContext = context;
        this.mViews = new SparseArray<View>();
        mConvertView = LayoutInflater.from(context).inflate(layoutId, parent,
                false);
        // setTag
        mConvertView.setTag(this);
    }

    /**
     * 拿到一个ViewHolder对象
     *
     * @param context
     * @param convertView
     * @param parent
     * @param layoutId
     * @param position
     * @return
     */
    public static ViewHolder get(Context context, View convertView,
                                 ViewGroup parent, int layoutId, int position)
    {
        if (convertView == null)
        {
            return new ViewHolder(context, parent, layoutId, position);
        }
        return (ViewHolder) convertView.getTag();
    }

    public View getConvertView()
    {
        return mConvertView;
    }

    /**
     * 通过控件的Id获取对于的控件，如果没有则加入views
     *
     * @param viewId
     * @return
     */
    public <T extends View> T getView(int viewId)
    {
        View view = mViews.get(viewId);
        if (view == null)
        {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    /**
     * 为TextView设置字符串
     *
     * @param viewId
     * @param text
     * @return
     */
    public ViewHolder setText(int viewId, String text)
    {
        TextView view = getView(viewId);
        view.setText(text);
        return this;
    }

    /**
     * 为TextView设置字符串
     *
     * @param viewId
     * @param
     * @return
     */
    public ViewHolder setTextColorAndSize(int viewId, int color, float size)
    {
        TextView view = getView(viewId);
        view.setTextColor(color);
        view.setTextSize(size);
        return this;
    }

    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @param drawableId
     * @return
     */
    public ViewHolder setImageResource(int viewId, int drawableId)
    {
        ImageView view = getView(viewId);
        view.setImageResource(drawableId);

        return this;
    }

    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @param
     * @return
     */
    public ViewHolder setImageBitmap(int viewId, Bitmap bm)
    {
        ImageView view = getView(viewId);
        view.setImageBitmap(bm);
        return this;
    }

    /**
     * 为ImageView设置图片
     * @param viewId
     * @param url
     * @return
     */
    public ViewHolder setImageUrl(int viewId, String url)
    {
        ImageView view = getView(viewId);

        Picasso.with(mContext).load(url).
                placeholder(R.drawable.default120).
                fit().centerCrop().
                into(view);

        return this;
    }

    public ViewHolder setImageUrl2(int viewId, String url)
    {
        ImageView view = getView(viewId);

        Picasso.with(mContext).load(url).
                placeholder(R.drawable.default120).
                fit().centerInside().
                into(view);

        return this;
    }

    public ViewHolder setViewVisable(int viewId, boolean visable){
        View view = getView(viewId);

        view.setVisibility(visable ? View.VISIBLE : View.GONE);

        return this;
    }

    public void setViewEnable(int viewId, boolean enable){
        View view = getView(viewId);
        view.setEnabled(enable);
    }

    public void setViewOnclick(int viewId, View.OnClickListener clickListener){

        View view = getView(viewId);
        view.setOnClickListener(clickListener);
    }

    public void setViewDoubleOnclick(int viewId, int mPosition, View.OnClickListener clickListener){

        View view = getView(viewId);
        view.setOnClickListener(clickListener);
    }

    public void setViewBG(int viewId, int bg){
        View view = getView(viewId);
        view.setBackgroundResource(bg);
    }

    public void setViewSelect(int viewId, boolean select){
        View view = getView(viewId);
        view.setSelected(select);
    }

    public int getPosition()
    {
        return mPosition;
    }

    public void setTvColor(int viewId, String color){
        TextView view = getView(viewId);
        view.setTextColor(Color.parseColor(color));
    }

    public void setVIewPressChange(int viewId, final boolean selected){
        final View view = getView(viewId);

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                view.setSelected(selected);
            }
        });
    }

    public void setViewCheck(int viewId, boolean check){
        CheckedTextView view = getView(viewId);
        view.setChecked(check);
    }
}
